package echo.demo2.codec;

import com.fasterxml.jackson.databind.ObjectMapper;
import echo.protocol.MessageProtocol;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

public class Demo2Encoder extends MessageToByteEncoder<MessageProtocol> {

    @Override
    protected void encode(ChannelHandlerContext ctx, MessageProtocol msg, ByteBuf out) throws Exception {
        ObjectMapper ob = new ObjectMapper();
        byte[] bytes = ob.writeValueAsBytes(msg);
        out.writeInt(bytes.length);
        out.writeBytes(bytes);

    }
}
